package flc.ast.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileTransferActivity;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.FragmentPictureBinding;
import flc.ast.utils.SendFileManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PictureFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PictureFragment extends BaseNoModelFragment<FragmentPictureBinding> {

    private SelPictureAdapter pictureAdapter;
    private SendFileManager mInstance;

    @Override
    protected int onCreate() {
        return R.layout.fragment_picture;
    }

    @Override
    protected void initView() {
        mInstance = SendFileManager.getInstance();

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext,4));
        pictureAdapter=new SelPictureAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        pictureAdapter.getItem(position).setChecked(!pictureAdapter.getItem(position).isChecked());
        pictureAdapter.notifyDataSetChanged();
        boolean hasExist = false;
        int tmpPosition = -1;
        for (SelectFileBean videoCollectionBean : mInstance.getSelectFileBeans()) {
            tmpPosition++;
            if (pictureAdapter.getItem(position).getUri().equals(videoCollectionBean.getPath())) {
                hasExist = true;
                break;
            }
        }
        if (hasExist) {
            mInstance.deleteSelectFile(tmpPosition);
        } else {
            SelectFileBean bean = new SelectFileBean();
            bean.setType(1);
            bean.setName(pictureAdapter.getItem(position).getMediaName());
            bean.setPath(pictureAdapter.getItem(position).getUri());
            bean.setSize(pictureAdapter.getItem(position).getSize());
            mInstance.addSelectFile(bean);
        }
        ((FileTransferActivity) mContext).refreshDataCount(mInstance);
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}